﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Linq;

namespace G.APITest.Config
{
    [DataContract]
    public class APIConfig : G.SL.Data.NotifyObject
    {
        public APIConfig()
        {
            this.Paramters = new ObservableCollection<Paramter>();
            this.Headers = new ObservableCollection<KVPair>();
        }
        private APIProjectConfig projectConfig;
        internal APIProjectConfig ProjectConfig
        {
            get
            {
                return projectConfig;
            }
            set
            {
                projectConfig = value;
            }
        }
        private string name;
        [DataMember(Name = "Name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                this.name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private string signPramterKey = "sign";
        [DataMember]
        public string SignPramterKey
        {
            get
            {
                if (string.IsNullOrWhiteSpace(signPramterKey))
                    return "sign";
                return signPramterKey;
            }
            set
            {
                signPramterKey = value;
                this.NotifyPropertyChanged("SignPramterKey");
            }
        }

        private string signRule;
        [DataMember]
        public string SignRule
        {
            get
            {
                return signRule;
            }
            set
            {
                signRule = value;
                this.NotifyPropertyChanged("SignRule");
            }
        }

        ///// <summary>
        ///// 暂时用于类型声称
        ///// </summary>
        //private string engname = "Root";
        //[XmlElement("EngName")]
        //public string EngName
        //{
        //    get
        //    {
        //        return engname;
        //    }
        //    set
        //    {
        //        this.engname = value;
        //        NotifyPropertyChanged("EngName");
        //    }
        //}

        [DataMember(Name = "Path")]
        public string Path { get; set; }

        [DataMember(Name = "Paramters")]
        //[XmlArrayItem("Paramter")]
        public ObservableCollection<Paramter> Paramters { get; set; }

        [DataMember(Name = "Headers")]
        //[XmlArrayItem("Header")]
        public ObservableCollection<KVPair> Headers { get; set; }

        [IgnoreDataMember]
        public bool IsOk { get; set; }

        [DataMember(Name = "IsPost")]
        public bool IsPost { get; set; }


        [DataMember(Name = "Description")]
        public string Description { get; set; }

        private string testresult;
        [IgnoreDataMember()]
        public string TestResult
        {
            get
            {
                return testresult;
            }
            set
            {
                testresult = value;
                NotifyPropertyChanged("TestResult");
            }
        }

        public System.Collections.Generic.Dictionary<string, string> GetAllParamters(APIProjectConfig config)
        {
            return (from c in this.Paramters where c.ParamterType != ParamterType.ByteArray select new KVPair() { Key = c.Name, Value = c.Value }).Union(config.GlobalParams).ToDictionary(c => c.Key, c => c.Value);
        }
    }



}
